package anthology.daily_question._24_12;

import java.util.PriorityQueue;

//https://leetcode.cn/problems/maximum-number-of-eaten-apples/description/?envType=daily-question&envId=2024-12-24
public class _1705吃苹果的最大数目 {
    class Solution {
        public int eatenApples(int[] apples, int[] days) {
            int ans = 0;
            PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
            int n = apples.length;
            int i = 0;
            while (i < n) {
                while (!pq.isEmpty() && pq.peek()[0] <= i) {
                    pq.poll();
                }
                int rottenDay = i + days[i];
                int count = apples[i];
                if (count > 0) {
                    pq.offer(new int[]{rottenDay, count});
                }
                if (!pq.isEmpty()) {
                    int[] arr = pq.peek();
                    arr[1]--;
                    if (arr[1] == 0) {
                        pq.poll();
                    }
                    ans++;
                }
                i++;
            }
            while (!pq.isEmpty()) {
                while (!pq.isEmpty() && pq.peek()[0] <= i) {
                    pq.poll();
                }
                if (pq.isEmpty()) {
                    break;
                }
                int[] arr = pq.poll();
                int curr = Math.min(arr[0] - i, arr[1]);
                ans += curr;
                i += curr;
            }
            return ans;
        }
    }
}
